package cn.itcast.Rainbow_self_mention.Activity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import cn.itcast.Rainbow_self_mention.R;
import cn.itcast.Rainbow_self_mention.Server.CountServer;
import cn.itcast.Rainbow_self_mention.Dao.SqlHelper;

public class Personal extends AppCompatActivity {
    Button out;
    TextView name;
    ImageView photo,home,upload,map;
    SqlHelper hp;
    Intent server;
    MyServiceConn conn;
    CountServer.sender sender;
    static String sentresult;
    static Boolean jump;
    static Boolean link;
    static TextView ed;
    int userid;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.low_mine);
        server = new Intent(this, CountServer.class);//创建意图对象
        conn = new MyServiceConn();//创建服务连接对象
        bindService(server, conn, BIND_AUTO_CREATE);
    }
    class MyServiceConn implements ServiceConnection { //用于实现连接服务
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            sender = (CountServer.sender) service;
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    }
    protected void onResume() {
        super.onResume();
        init();
        initbutton();
        initinfo();
    }
    public void init() {
    out=findViewById(R.id.my_exit);
    name=findViewById(R.id.my_username);
    photo=findViewById(R.id.my_photo);
    home=findViewById(R.id.login_homepage);
    upload=findViewById(R.id.login_deposit);
    map=findViewById(R.id.login_map);
    ed = findViewById(R.id.ed);
    hp=new SqlHelper(this);
        jump=false;
        link=false;

    }
    public void threadwait(int time) {
        try {
            Thread.sleep(time * 1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    //监听属性值判断是否跳转
    class Swatch extends Thread {
        @Override
        public void run() {
            threadwait(1);

            if(link)
            {
                if(jump)
                {
                    Looper.prepare();
                    Transaction t=new Transaction();
                    t.start();
                    Looper.loop();
                }
                else
                {
                        Looper.prepare();
                        Toast.makeText(Personal.this,"退出失败", Toast.LENGTH_SHORT).show();
                        Looper.loop();
                }
            }

            else {
                Looper.prepare();
                Toast.makeText(Personal.this,"网络连接失败\n请检查网络", Toast.LENGTH_SHORT).show();
                sender.relink();
                Looper.loop();
            }

        }
    }
    //跳转函数
    class Transaction extends Thread {
        @Override
        public void run() {
            super.run();
            Intent intent = new Intent(Personal.this, Login.class);
            startActivity(intent);
            overridePendingTransition(0, 0);
        }
    }
    public void initinfo() {
        SQLiteDatabase db=hp.getReadableDatabase();
        Cursor cursor=db.query("logined",null,null,null,null,null,null);
        cursor.moveToFirst();
        name.setText(cursor.getString(cursor.getColumnIndex("username")));
        userid=cursor.getInt(cursor.getColumnIndex("userid"));
        cursor.close();
        db.close();
    }
    public void initbutton(){
        upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(Personal.this, Order.class);
                startActivity(intent);
                overridePendingTransition(0, 0);
            }
        });

        home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(Personal.this, Home.class);
                startActivity(intent);
                overridePendingTransition(0, 0);
            }
        });

        map.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(Personal.this, Map.class);
                startActivity(intent);
                overridePendingTransition(0, 0);
            }
        });
        out.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sender.Exist(userid);
                Swatch swatch=new Swatch();
                swatch.start();
            }
        });
    }
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent home = new Intent(Intent.ACTION_MAIN);
            home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            home.addCategory(Intent.CATEGORY_HOME);
            startActivity(home);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    public static Handler handler = new Handler()
    {
        public void handleMessage(Message msg)
        {
            Bundle bundle = msg.getData();
            sentresult=bundle.getString("sentresult");
            if(sentresult!=null)
            {
                if (sentresult.equals("网络连接错误")) {
                    link = false;
                }
                if (sentresult.equals("网络连接成功")) {
                    link = true;
                }
                if(sentresult.equals("退出成功"))
                {
                    jump=true;
                }
                if(sentresult.equals("退出失败"))
                {
                    jump=false;
                }
            }
        }

    };

}
